
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QSettings>

#include "NodeBase.h"
#include "BranchBase.h"

#include "GraphicsScene.h"
#include "GraphicsView.h"

#include "T2C/cs_t2cmanager.h"

class TableModel;
//class T2CManager;

class MainWindow : public QMainWindow
{
    Q_OBJECT

    QMenuBar    * menuBar;
    QToolBar    * toolPalette;
    QToolBar    * visibleBar;
    QToolBar    * toolsBar;
    QStatusBar  * statusBar;
    QMenu       * fileMenu;
    QMenu       * editMenu;
    QMenu       * toolsMenu;
    QMenu       * propertiesMenu;
    QMenu       * settingsMenu;
    QMenu       * chartsMenu;

    QVector< double > U1;
    QVector< double > U5;
    QVector< double > U7;
    QVector< double > U11;
    QVector< double > U13;
    QVector< double > U17;
    QVector< double > Q1;
    QVector< double > Q5;
    QVector< double > Q7;
    QVector< double > Q11;
    QVector< double > Q13;
    QVector< double > Q17;
    QVector< double > P;
    int metricsCount;
    int NodesCount;

    void loadBinaryData( const QString & fName, QVector< double > * vData )
    {
        NodesCount = 63;
        metricsCount = 0;
        vData->clear();

        FILE * fd = fopen( fName.toStdString().c_str(), "rb" );

        if( fd )
        {
            float data [ NodesCount ];

            fseek( fd, 0L, SEEK_SET );
            fread( &metricsCount, sizeof( metricsCount ), 1, fd );
            fread( &data, sizeof( data ), 1, fd );

            for( int i=0; i < metricsCount; ++i )
            {
                fseek( fd, (sizeof(metricsCount)+i*512), SEEK_SET );
                fread( &data, sizeof( data ), 1, fd );
                for( int j=0; j < NodesCount; ++j )
                    vData->append( data[ j ] );

            }
            fclose( fd );
        }
    }

    QHash< int, QList< float > > hrmHash;
    QHash< int, QList< float > > pwrLossHash;
    QHash< int, QList< float > > voltageHash;

    QToolBar * hrmBar;
    QList< QAction* > hrmActList;

    QToolBar * pwrLossBar;
    QList< QAction* > pwrLossActList;

    QToolBar * voltageBar;
    QList< QAction* > voltageActList;

    QAction * nodeProps;
    QAction * branchProps;
    QAction * tagProps;
    QAction * updateTagPvs;

    QAction * chartU;
    QAction * chartI;
    QAction * chartP;
    QAction * chartHRMI;
    QAction * chartHRMU;
    QAction * chartTDHI;
    QAction * chartTDHU;

    GraphicsScene   * scene;
    GraphicsView    * view;

    QMap< int, QColor > visibleBarMap;

    TableModel * nodesTableMdl;
    TableModel * branchesTableMdl;

    T2CManager * manager;
    QString host;
    int port;

    QStringList paramsToTag;
    QStringList chartDataSrtList;
    QString csv;

    QSettings * mainSettings;
    QString iniFile;

    enum SettingType
    {
        stALL_DATA    = 0,
        stXML         = 1
    };


public:
    explicit MainWindow( QWidget * parent = 0 );
    ~MainWindow();

    void DrawMap();
    
private:
    void createToolPalette();
    void createMenuBar();
    void createStatusBar();
    void createVisibleBar();
    void createToolsBar();
    void createHrmBar();
    void fillHrmHashes( bool showDialog = true );
    void createPwrLossBar();
    void createVoltageBar();
    void createAllHrmBars();

    void deployBtnsOnVisibleBar();
    void setCaption( QString cap = "" );

    void makeNodesModel();
    void makeBranchesModel();

    bool checkHostPort();
    //int showHarmonicsSettingsDialog();
    void updateBtnTagPv();

    void readSourceForChart();

    void colorizeNodesByHrm( QList< float > * values, QList< Node* > * nodes );
    QColor getColorBetweenValues( float minVal, float maxVal, float curVal );

    void setNodesMainView();

    void setDefaultHrmBarView();
    void setDefaultPwrBarView();
    void setDefaultVltBarView();

private slots:
    void sltNewFile();
    void sltOpenFile( bool showDialog = true );
    void sltSaveFile();
    void sltSaveFileAs();
    void sltEditMenuClick( QAction * a );
    void sltToolsMenuClick( QAction * a );
    void sltToolPaletteClick( QAction * a );
    void sltPropertiesMenuClick( QAction * a );
    void sltSettingsMenuClick( QAction * a );
    void sltChartsMenuClick( QAction * a );
    void OnVisibleBarBtnClick( QAction * a );
    void OnHrmBarBtnClick( QAction * a );
    void OnPwrLossBarBtnClick( QAction * a );
    void OnVoltageBarBtnClick( QAction * a );
    void OnToolsBarBtnClick( QAction * a );
    void sltUpdateVisibleBar();
    void sltUpdateMenuProps();

    void sltShowQ3D( int nodeNum );
    void sltShowU3D( int nodeNum );
    void sltShowHistogram( int nodeNum );

    //void sltReceiveCSRespose( QString txt );
    void sltT2CManConnected();
    void sltExecCmdList( QStringList cmd );
    void sltReceivePV( quint64 idf );
    void sltManagerConnected();
    void sltManagerDisconnected();


    void openIniFile();
    void saveSettings( SettingType st = stALL_DATA );
    void loadSettings();

signals:
    void sgnExecCmdList( QStringList cmd );

};

#endif // MAINWINDOW_H
